package cn.tj.controller;

import cn.tj.entity.PageBean;
import cn.tj.entity.Result;
import cn.tj.entity.Type;
import cn.tj.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    /**
     * 获取所有类型列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<Type>> list(){
        List<Type> tList=typeService.list();
        return Result.success(tList);
    }

    /**
     * 条件分页获取信息
     * @param currentPage
     * @param pageSize
     * @param minPrice
     * @param maxPrice
     * @return
     */
    @GetMapping("/listByPage")
    public Result<PageBean<Type>> listByPage(
            Integer currentPage,
            Integer pageSize,
            @RequestParam(required = false) Double minPrice,//最小价
            @RequestParam(required = false) Double maxPrice//最大价
    ){
        PageBean<Type> pg = typeService.listByPage(currentPage,pageSize,minPrice,maxPrice);
        return Result.success(pg);
    }

    /**
     * 通过id查询信息
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public Result<Type> getById(Integer id){
        Type type = typeService.getById(id);
        return Result.success(type);
    }

    /**
     * 编辑信息
     * @param type
     * @return
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody Type type){
        typeService.edit(type);
        return Result.success();
    }

    /**
     * 新增
     * @param type
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Type type){
        typeService.add(type);
        return Result.success();
    }

    /**
     * 通过id删除信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id){
        typeService.deleteById(id);
        return Result.success();
    }
}
